let logId = sessionStorage.getItem('logId')
let userInfo;
let newAvator;
//查询当前用户的相关信息
$.ajax({
  url: '/perSet/get',
  method:"GET",
  data:{
    logId
  }
})
  .done((req,res)=>{
    console.log('请求成功', req.data);
    userInfo = req.data;
    renderingPage();

    // $('#goods_name2 option:selected').val(newvalue);
  })
  .fail((err) => {
    console.log('请求失败：',err);
   })








//渲染页面,将当前用户的数据的数据添加到表格里
function renderingPage(){
  $('.wf-input-name').val(userInfo.nickname)
  $('.wf-input-email').val(userInfo.email)
  $('.wf-input-tel').val(userInfo.phone)
//将部门列表获取并渲染
  $.ajax({
    url: '/perSet/getdept',
    method: "GET",
  })
    .done((req, res) => {
      console.log('请求成功', req.data);
      let dept = req.data;
      for (var i = 0; i < dept.length; i++) {
        var option = `<option value="${dept[i].deptId}">${dept[i].deptName}</option>`
        $('.wf-input-department').append(option);
      }

      $('.wf-input-department').val(userInfo.deptId);
      let newAvator = userInfo.avator;
      $('.wf-img-src').css('background', 'url(' + newAvator+') no-repeat center /cover')

    })
    .fail((err) => {
      console.log('请求失败：', err);
    })
  
}
//头像上传

$('#ajaxBtn').on('click',()=>{
  console.log($('#ajaxImg')[0].files)
  const file = $('#ajaxImg')[0].files[0];
  //FormData用于去处理multipart/foem-data
  let formData = new FormData();
  formData.append('avator', file)//添加数据
  formData.append('userId', 1234567)//添加数据

  $.ajax({
    url: '/perSet/uploadavator',
    method: 'POST',
    data: formData,//上传的数据
    mimeType: 'multipart/form-data',//上传的数据类型
    processData: false,//不让jQuery 去出路发送的数据
    contentType: false,//不去设置请求头
  })
    .done(function (res) {
      let returnData = JSON.parse(res);
      newAvator = 'http://'+returnData.data.url
      
      console.log(returnData.data.url);
      $('.wf-img-src').css('background', 'url(' + newAvator + ') no-repeat center /cover')
    })
    .fail(function (err) {
      console.log(err)
    })
})
//点击取消
$('.wf-cancel').on('click',  ()=> {
  renderingPage()
})

//点击保存
$('.wf-sava').on('click',  ()=> {
  //获取下拉列表的选择值
  let nikenameVal = $('.wf-input-name').val();
  let emialVal = $('.wf-input-email').val();
  let phoneVal = $('.wf-input-tel').val();
  let deptVal=$('option:selected')[0].value;
  let passwordValA = $('.wf-password-a').val();
  let passwordValB = $('.wf-password-b').val();


  let emailPat = /^([a-z0-9_\.-]+)@([\da-z\.-]+)\.([a-z\.]{2,6})$/;
  if (!(emailPat.test(emialVal))){
    alert('邮箱格式不正确');
    return
  }

  if (!passwordValA){
    alert('请输入密码');
    return
  }
  var passWordPat = /^[a-z0-9_-]{6,18}$/;
  if (!(passWordPat.test(passwordValB))) {
    alert('密码格式不正确')
    return;
  }

  //判断两次密码是否一致
  if (passwordValA == passwordValB){

    $.ajax({
      url:"/perSet/edit",
      method: "POST",
      data:{
        nikenameVal,
        emialVal,
        phoneVal,
        deptVal,
        passwordValB,
        newAvator,
        logId
      }
    })
    .done((res)=>{
      console.log(res.msg)
    })
    .fail((err)=>{
      console.log(err);
    })

    
    alert('保存成功');
    //清空密码输入框
    $('.wf-password-a').val('');
    $('.wf-password-b').val('');
  }else{
    alert('两次输入的密码不一致')
  }
  
})